package com.inspur.gs.sbssdl.sbyxgl.djjl.djjl.back.bizactions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.parameter.retrieve.RetrieveParam;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
public class sbdjjlAddInspectionRecordMgrAction extends AbstractManagerAction<String> {
    private String inspectionRecord;

    public sbdjjlAddInspectionRecordMgrAction(IBEManagerContext managerContext, String inspectionRecord) {
        super(managerContext);
        this.inspectionRecord = inspectionRecord;
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String configID = "ebc8337d-ffb6-4b68-a314-d949ab38a00f";
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = null;
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(configID);
            map = mapper.readValue(inspectionRecord, Map.class);
            IEntityData data = lcp.retrieveDefault();

            String dataid = data.getID().toString();
            List<Map<String, Object>> detailsList = (List<Map<String, Object>>) map.get("sbdjjlmxs");

            ModifyChangeDetail curChange = new ModifyChangeDetail(data.getID());

            curChange.getPropertyChanges().put("sbid", map.get("sbid"));
            curChange.getPropertyChanges().put("sbbh", map.get("sbbh"));
            curChange.getPropertyChanges().put("sbmc", map.get("sbmc"));
            if (map.get("sbbz") != null) {
                curChange.getPropertyChanges().put("sbbz", map.get("sbbz"));
            }
            if (map.get("ggxh") != null) {
                curChange.getPropertyChanges().put("ggxh", map.get("ggxh"));
            }
            if (map.get("xh") != null) {
                curChange.getPropertyChanges().put("xh", map.get("xh"));
            }
            if (map.get("shopName") != null) {
                curChange.getPropertyChanges().put("shopName", map.get("shopName"));
            }
            if (map.get("sbbh_old") != null) {
                curChange.getPropertyChanges().put("sbbh_old", map.get("sbbh_old"));
            }
            if (map.get("lbmc") != null) {
                curChange.getPropertyChanges().put("lbmc", map.get("lbmc"));
            }
            if (map.get("ztmc") != null) {
                curChange.getPropertyChanges().put("ztmc", map.get("ztmc"));
            }
            if (map.get("abcmc") != null) {
                curChange.getPropertyChanges().put("abcmc", map.get("abcmc"));
            }
            if (map.get("bmmc") != null) {
                curChange.getPropertyChanges().put("bmmc", map.get("bmmc"));
            }
            if (map.get("bz") != null) {
                curChange.getPropertyChanges().put("bz", map.get("bz"));
            }
            //反馈人/部门相关信息
            curChange.getPropertyChanges().put("fkbmbh", map.get("fkbmbh"));
            curChange.getPropertyChanges().put("fkbmmc", map.get("fkbmmc"));

            Map<String, Object> fkridMap = (Map<String, Object>) map.get("fkrid");
            AssociationInfo fkrinfo = new AssociationInfo();
            fkrinfo.setValue("fkrid", fkridMap.get("fkrid"));
            curChange.getPropertyChanges().put("fkrid", fkrinfo);
            curChange.getPropertyChanges().put("fkrbh", map.get("fkrbh"));
            curChange.getPropertyChanges().put("fkrmc", map.get("fkrmc"));

            String djsj = map.get("djsj").toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(djsj);
            curChange.getPropertyChanges().put("djsj", date);

            for (Map<String, Object> detail : detailsList) {
                //新增一条子表数据
                ArrayList<String> nodeCodes = new ArrayList();
                nodeCodes.add("sbdjjlmx");
                ArrayList<String> hierarchyIds = new ArrayList();
                hierarchyIds.add(dataid);
                IEntityData entityData = lcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

                //子表关联
                ModifyChangeDetail change = new ModifyChangeDetail(entityData.getID());

                curChange.getPropertyChanges().put("djsj", sdf.parse(djsj));

                change.getPropertyChanges().put("djjg", detail.get("djjg"));
                change.getPropertyChanges().put("djbw", detail.get("djbw") != null ? detail.get("djbw") : "");
                change.getPropertyChanges().put("djnr", detail.get("djnr") != null ? detail.get("djnr") : "");
                change.getPropertyChanges().put("djresult", detail.get("djresult") != null ? detail.get("djresult") : "");
                change.getPropertyChanges().put("ycyy", detail.get("ycyy") != null ? detail.get("ycyy") : "");
                change.getPropertyChanges().put("ycms", detail.get("ycms") != null ? detail.get("ycms") : "");
                change.getPropertyChanges().put("yclx", detail.get("yclx") != null ? detail.get("yclx") : "");
                change.getPropertyChanges().put("bwsm", detail.get("bwsm") != null ? detail.get("bwsm") : "");
                change.getPropertyChanges().put("zysx", detail.get("zysx") != null ? detail.get("zysx") : "");

                curChange.addChildChangeSet("sbdjjlmx", change);
            }

            lcp.modify(curChange);
            lcp.save();

            // 强制从数据库取数，刷新数据
            RetrieveParam retrieveParam = new RetrieveParam();
            retrieveParam.setForceFromRepository(true);
            lcp.retrieve(dataid, retrieveParam);

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}